import 'package:flutter/material.dart';
import 'package:game_11_sleep_game11/file.dart';
import 'package:game_11_sleep_game11/menu.dart';

class rank_home extends StatelessWidget {
  const rank_home({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          onPressed: (){
            Navigator.push(context, MaterialPageRoute(builder: (context) => menu_home()));
          }, icon: Icon(Icons.arrow_left),
        ),
        centerTitle: true,
        title: Text("排行榜"),
      ),
      body: Body(),
    );
  }
}

class Body extends StatefulWidget {
  const Body({super.key});

  @override
  State<Body> createState() => _BodyState();
}

class _BodyState extends State<Body> {

  var l = [];

  @override
  void initState() {
    // TODO: implement initState
    var dic = read_rank_file();
    if(dic.isNotEmpty){
      l = dic.values.toList();
      l.sort((a, b){
        if(a[2] == b[2]){
          return a[1] - b[1];
        }
        else if(a[2]){
          return -1;
        }
        else{
          return 1;
        }
      });
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return ListView(
      children: [
        ListTile(
          title: Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              Text("名次"),
              Text("玩家名"),
              Text("时间"),
              Text("输赢"),
            ],
          ),
        ),
        for(int i = 0; i < l.length; i++)
          ListTile(
            title: Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Text("${i + 1}"),
                Text("${l[i][0]}"),
                Text("${l[i][1] ~/ 3600}:${(l[i][1] % 3600) ~/ 60}:${l[i][1] % 60}"),
                Text("${l[i][2]}"),
              ],
            ),
          ),
      ],
    );
  }
}
